package com.qiezi.project.dialog;

import android.app.Dialog;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hjq.base.BaseDialog;
import com.hjq.base.BaseDialogFragment;
import com.qiezi.R;

import org.w3c.dom.Text;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2018/12/2
 *    desc   : 消息对话框
 */
public class CreateUserDialog {

    public static class Builder
            extends BaseDialogFragment.Builder<Builder>
            implements View.OnClickListener {

        private OnListener mListener;





        public Builder(FragmentActivity activity) {
            super(activity);
            initialize();
        }

        public Builder(FragmentActivity activity, int themeResId) {
            super(activity, themeResId);
            initialize();
        }

        private EditText etUser,etPwd,etAddress;
        private TextView tvCreate,tvOther,tvClose,tvTitle;
        private View llWithdrawal;

        private void initialize() {
            setContentView(R.layout.dialog_create_user);
            setAnimStyle(BaseDialog.AnimStyle.SCALE);
            setGravity(Gravity.CENTER);

            etUser = findViewById(R.id.et_user);
            etPwd = findViewById(R.id.et_pwd);
            etAddress = findViewById(R.id.et_address);

            tvCreate = findViewById(R.id.tv_create);
            tvOther = findViewById(R.id.tv_other);
            tvClose = findViewById(R.id.tv_close);
            tvTitle = findViewById(R.id.tv_title);

            llWithdrawal = findViewById(R.id.ll_withdrawal);

            tvCreate.setOnClickListener(this);
            tvOther.setOnClickListener(this);
            tvClose.setOnClickListener(this);
        }




        public Builder setListener(OnListener l) {
            mListener = l;
            return this;
        }


        /**
         * {@link View.OnClickListener}
         */
        @Override
        public void onClick(View v) {
            if (mListener == null) return;

            if (v == tvCreate) {
                if (llWithdrawal.getVisibility() == View.VISIBLE){
                    mListener.create(getDialog(),etUser.getText().toString(),etPwd.getText().toString(),etAddress.getText().toString());
                }else {
                    mListener.login(getDialog(),etUser.getText().toString(),etPwd.getText().toString());
                }

            }else if (v == tvOther) {
                switchPage();
            }else if(v == tvClose){
                mListener.cancel(getDialog());
                dismiss();
            }
        }

        private void switchPage(){
            if (llWithdrawal.getVisibility() != View.VISIBLE){
                llWithdrawal.setVisibility(View.VISIBLE);
                tvTitle.setText("创建推广账号");
                tvCreate.setText("确认创建");
                tvOther.setText("已有账号直接登录");
                etUser.setHint(R.string.createUser);
                etPwd.setHint(R.string.createPwd);
            }else {
                llWithdrawal.setVisibility(View.GONE);
                tvTitle.setText("登录推广账号");
                tvCreate.setText("登录");
                tvOther.setText("去创建一个账号");
                etUser.setHint(R.string.loginUser);
                etPwd.setHint(R.string.loginPwd);
            }
        }
    }


    public interface OnListener {

        /**
         * 点击注册时回调
         */
        void create(Dialog dialog,String user,String pwd,String address);

        /**
         * 点击登录时回调
         */
        void login(Dialog dialog,String user,String pwd);
        /**
         * 点击取消时回调
         */
        void cancel(Dialog dialog);
    }
}